﻿using System;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Practices.ServiceLocation;
using SAFPodcast.WP8.DTO.Entities;
using SAFPodcast.WP8.Model.Services;

namespace SAFPodcast.WP8.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class EditFeedViewModel : ViewModelBase
    {
        private IDataService _dataService;

        /// <summary>
        /// Initializes a new instance of the EditFeedViewModel class.
        /// </summary>
        public EditFeedViewModel(IDataService dataService)
        {
            _dataService = dataService;
            saveHidden = new RelayCommand(Save);
            cancelHidden = new RelayCommand(Cancel);
            testFeed = new RelayCommand(TestFeedMethod);
        }

        #region SelectedFeed
        public const string SelectedFeedPropertyName = "SelectedFeed";
        private Feed selectedFeed = null;
        public Feed SelectedFeed
        {
            get
            {
                return selectedFeed;
            }
            set
            {
                Set(SelectedFeedPropertyName, ref selectedFeed, value);
            }
        }
        #endregion
        #region Name
        public const string NamePropertyName = "Name";
        private string name = null;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                Set(NamePropertyName, ref name, value);
            }
        }
        #endregion
        #region UrlAsString
        public const string UrlAsStringPropertyName = "UrlAsString";
        private string urlAsString = null;
        public string UrlAsString
        {
            get
            {
                return urlAsString;
            }
            set
            {
                Set(UrlAsStringPropertyName, ref urlAsString, value);
            }
        }
        #endregion
        #region TestResult
        private const String TestResultPropertyName = "TestResult";
        private String testResult;
        public String TestResult
        {
            get { return testResult; }
            set
            {
                Set(TestResultPropertyName, ref testResult, value);
            }
        }
        #endregion
        #region TestFeed
        private RelayCommand testFeed;
        public RelayCommand TestFeed
        {
            get { return testFeed; }
        }

        public void TestFeedMethod()
        {
            Uri uri = null;
            if (Uri.TryCreate(UrlAsString, UriKind.Absolute, out uri))
            {
                _dataService.TestFeed(uri, TestFeedCallback);
            }
            else
            {
                MessageBox.Show("The URL is not valid.", "URI", MessageBoxButton.OK);
            }
        }

        public void TestFeedCallback(bool result)
        {
            DispatcherHelper.RunAsync(() =>
            {
                if (result)
                {
                    TestResult = "Success !";
                }
                else
                {
                    TestResult = "Failed...";
                }
            });
        }
        #endregion
        #region CancelHidden
        private RelayCommand cancelHidden;
        public RelayCommand CancelHidden
        {
            get { return cancelHidden; }
        }

        public void Cancel()
        {
            this.MessengerInstance.Send(false, "NavigationBackRequest");
        }
        #endregion
        #region SaveHidden
        private RelayCommand saveHidden;
        public RelayCommand SaveHidden
        {
            get { return saveHidden; }
        }

        public void Save()
        {
            Uri uri = null;
            if (Uri.TryCreate(UrlAsString, UriKind.Absolute, out uri))
            {
                var newFeed = _dataService.UpdateFeed(SelectedFeed, UrlAsString, Name);
                var vm = ServiceLocator.Current.GetInstance<MainViewModel>();
                vm.Feeds.Remove(SelectedFeed);
                vm.Feeds.Add(newFeed);
                this.MessengerInstance.Send(false, "NavigationBackRequest");
            }
            else
            {
                MessageBox.Show("The URL is not valid.", "URI", MessageBoxButton.OK);
            }
        }
        #endregion
    }
}
